<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>园田家</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="记录学习上遇到的各种难题">
<meta property="og:type" content="website">
<meta property="og:title" content="园田家">
<meta property="og:url" content="http://example.com/index.html">
<meta property="og:site_name" content="园田家">
<meta property="og:description" content="记录学习上遇到的各种难题">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="kurodasense">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="园田家" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">园田家</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="post-leetcode-26" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/04/18/leetcode-26/" class="article-date">
  <time datetime="2021-04-18T15:26:44.000Z" itemprop="datePublished">2021-04-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2021/04/18/leetcode-26/">leetcode-26</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>今天的力扣每日一题《26.删除有序数组中的重复项》。难度是简单。之前我就做过了《删除有序数组中的重复项II》，<br>所以这道简单题，我做起来也比较简单。</p>
<hr>
<h4 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h4><p>给你一个有序数组 <code>nums</code> ，请你 <strong>原地</strong> 删除重复出现的元素，使每个元素 <strong>只出现一次</strong> ，<br>返回删除后数组的新长度。</p>
<p>不要使用额外的数组空间，你必须在 <strong>原地</strong> 修改输入数组 并在使用 <strong>O(1)</strong> 额外空间的条件下完成。</p>
<hr>
<p><strong>说明：</strong><br>为什么返回数值是整数，但输出的答案是数组呢?<br>请注意，输入数组是以 <strong>「引用」</strong>方式传递的，这意味着在函数里修改输入数组对于调用者是可见的。<br>你可以想象内部操作如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; nums 是以“引用”方式传递的。也就是说，不对实参做任何拷贝</span><br><span class="line">int len &#x3D; removeDuplicates(nums);</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 在函数里修改输入数组对于调用者是可见的。</span><br><span class="line">&#x2F;&#x2F; 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。</span><br><span class="line">for (int i &#x3D; 0; i &lt; len; i++) &#123;</span><br><span class="line">    print(nums[i]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>示例1：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">输入：nums &#x3D; [1,1,2]</span><br><span class="line">输出：2, nums &#x3D; [1,2]</span><br><span class="line">解释：函数应该返回新的长度 2 ，并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。</span><br></pre></td></tr></table></figure>
<p><strong>示例2：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">输入：nums &#x3D; [0,0,1,1,1,2,2,3,3,4]</span><br><span class="line">输出：5, nums &#x3D; [0,1,2,3,4]</span><br><span class="line">解释：函数应该返回新的长度 5 ， 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。</span><br></pre></td></tr></table></figure>
<p>来源：力扣（LeetCode）<br>链接：<a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array">https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array</a><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<hr>
<p>根据题目的描述和它给的说明。这个数组已经是升序的，所以我们可以把重复的数字都往后移，而把不重复的那部分都放到数组的前面。<br>我们可以设置一个 <code>left</code> 指针和 <code>cur</code> 指针。 <code>left</code> 用来管理已经处理好的不重复数字，而 <code>cur</code> 就是往后遍历。<br>一开始，<code>left</code> 指向数组的第一个元素，<code>cur</code> 指向数组的第二个元素。<br>随后，如果 <code>nums[left] == nums[cur]</code> ， <code>cur</code> 就继续往后遍历，即 <code>cur++</code> 。<br>当遇到 <code>nums[left] != nums[cur]</code> 时，我们让 <code>left</code> 往后移一格。因为 <code>left</code> 是用来管理处理好的不重复数字，所以 <code>left</code> 往后移一格，就是用来存放下一个不重复数字。<br>然后，让 <code>nums[left]</code> 和 <code>nums[cur]</code> 交换数字。这样就能让重复的数字往后移，而让不重复的数字放数组前面了。当然，处理完这个过程后，还得让 <code>cur++</code> ，让 <code>cur</code> 指向下一个数字，继续迭代上面的过程。<br><strong>代码：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">removeDuplicates</span><span class="params">(<span class="keyword">int</span>[] nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = nums.length;</span><br><span class="line">        <span class="keyword">int</span> left = <span class="number">0</span> ;</span><br><span class="line">        <span class="keyword">if</span>(n == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> cur = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span>(cur &lt; n)&#123;</span><br><span class="line">            <span class="keyword">if</span>(nums[cur] == nums[left])&#123;</span><br><span class="line">                cur++;</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                left++;</span><br><span class="line">                <span class="keyword">int</span> temp = nums[left];</span><br><span class="line">                nums[left] = nums[cur];</span><br><span class="line">                nums[cur] = temp;</span><br><span class="line">                cur++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> left + <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><del>本来想画一个图来表达这个过程的，但是想了想又觉得画图好麻烦，并且不知道用什么图画。最后，还是没画了，，，</del></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/04/18/leetcode-26/" data-id="cknnbp0sf0000l4v5bg30ek1o" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-hasNext-与hasNextLine" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/03/21/hasNext-%E4%B8%8EhasNextLine/" class="article-date">
  <time datetime="2021-03-21T11:25:39.000Z" itemprop="datePublished">2021-03-21</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2021/03/21/hasNext-%E4%B8%8EhasNextLine/">hasNext()与hasNextLine()</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>今天下午参加了院里的天梯赛选拔兼蓝桥杯热身赛。正好我刷力扣也刷了一个月了，并且我又报了上半年的<br>蓝桥杯，为了看一下自己的算法水平有没有进步和抱着学东西的态度，就去了。结果，很惨啊：<br><img src="/images/hasNextAndHasNextLine/rank.jpg" alt="排名记录"><br>10道题里面，有6道题是有思路的。第一道和第三道都很简单。然后第二道题想不出来，直接算是能算出来的，<br>但是就是想不出式子，最后用了贪心思想得了一半分。接下来的第四道和第六道题，有思路，但是写不出来（就是不会做了）。<br>而最后一道题，就是今天的主题。我写出来了，而题目没有说输入的结束条件，然后我想了半天该怎么结束输入啊。最后想到比赛结束了，<br>我也没能想出来。<br>回到宿舍后，经两位舍友的点醒，重新修改了一下代码后，题目通过了。  </p>
<hr>
<p>总之先看一下<strong>题目</strong>：<br>代码中如果出现了两行上下对应的 <code>begin</code> 和 <code>end</code> （这两行中可见字符只包含 <code>begin</code>或<br> <code>end</code> ），则这两行与这两行之间的代码距离行首会多出两个空格。题目保证<code>begin</code>和<code>end</code>是上下对应的。</p>
<h4 id="输入"><a href="#输入" class="headerlink" title="输入"></a>输入</h4><p>输入多行文本，单行文本不超过 <code>1000</code> 个字符，不超过 <code>100</code> 行，数据保证不会有空行。  </p>
<h4 id="输出"><a href="#输出" class="headerlink" title="输出"></a>输出</h4><p>输出进行缩进处理后的文本。  </p>
<h5 id="样例输入"><a href="#样例输入" class="headerlink" title="样例输入"></a>样例输入</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">apioe(@)(#!</span><br><span class="line">begin</span><br><span class="line">029348aoew</span><br><span class="line">   begin</span><br><span class="line">end</span><br><span class="line">		end</span><br><span class="line">		epse</span><br><span class="line">begin</span><br><span class="line">beginee</span><br><span class="line">end3o8w8*</span><br><span class="line">end</span><br></pre></td></tr></table></figure>
<h5 id="样例输出"><a href="#样例输出" class="headerlink" title="样例输出"></a>样例输出</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">apioe(@)(#!</span><br><span class="line">  begin</span><br><span class="line">  029348aoew</span><br><span class="line">    begin</span><br><span class="line">    end</span><br><span class="line">  end</span><br><span class="line">epse</span><br><span class="line">  begin</span><br><span class="line">  beginee</span><br><span class="line">  end3o8w8*</span><br><span class="line">  end</span><br></pre></td></tr></table></figure>
<p>很简单，下面是我修改过的代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="keyword">import</span> java.util.Stack;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">showhang</span><span class="params">(String string, <span class="keyword">int</span> count)</span></span>&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;count;i++)&#123;</span><br><span class="line">			System.out.print(<span class="string">&quot; &quot;</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		System.out.println(string);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		Scanner sc = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">		<span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">		String string;</span><br><span class="line">		Stack&lt;String&gt; stack = <span class="keyword">new</span> Stack&lt;String&gt;();</span><br><span class="line">		<span class="keyword">while</span>(sc.hasNextLine())&#123;</span><br><span class="line">			string = sc.nextLine();</span><br><span class="line">			string = string.trim();</span><br><span class="line">			<span class="keyword">if</span>(string.equals(<span class="string">&quot;begin&quot;</span>))&#123;</span><br><span class="line">				count += <span class="number">2</span>;</span><br><span class="line">				showhang(string, count);</span><br><span class="line">				stack.push(string);</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(string.equals(<span class="string">&quot;end&quot;</span>))&#123;</span><br><span class="line">				showhang(string, count);</span><br><span class="line">				count -= <span class="number">2</span>;</span><br><span class="line">				stack.pop();</span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				showhang(string, count);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<p>为什么当时我没通过呢，因为我的输入结束条件没写好。我当时忘了有一个<code>hasNextLine()</code>用来检测是否还有输入，<br>所以提交上去的时候，系统就一直提示运行错误（因为无法跳出循环嘛）。然后，估计还有一个原因是我之前写的是<code>string = sc.next()</code>，<br>而不是用<code>sc.nextLine()</code>接收字符串。<br>题目通过了后，我就有疑惑了：1.<code>nextLine()</code>和<code>Next()</code>有什么区别？2.<code>hasNext()</code>和<code>hasNextLine()</code>是怎么判断接下来有无输出的，它返回的值是什么？<br>带着这些疑问，我查了半小时百度后，总算有答案了：</p>
<hr>
<h3 id="问题一：nextLine-和next-有什么区别？"><a href="#问题一：nextLine-和next-有什么区别？" class="headerlink" title="问题一：nextLine()和next()有什么区别？"></a>问题一：<code>nextLine()</code>和<code>next()</code>有什么区别？</h3><p>根据<strong>菜鸟教程</strong>上面的解释来讲，<code>next()</code>和<code>nextLine()</code>都是可以获得输入流中的字符串。但是<code>next()</code>在遇到空格的时候，就会停止获取空格后面的字符串，简而言之，<br>就是不能获取带空格的字符串；而<code>nextLine()</code>则可以获取带空格的字符串。其实从字面意思上讲，我觉得<code>nextLine()</code>就是指<strong>获取下一行，即获取一行的东西</strong>，<code>next()</code>就是指<strong>获取下一个，<br>即一块没有间隔的东西</strong>，这个间隔可以理解为<strong>空格</strong>。所以，<code>next()</code>在遇到空格时会停下，而<code>nextLine()</code>遇到回车时才会停下。</p>
<h3 id="问题二：hasNext-和hasNextLine-是怎么判断接下来有无输出的，它返回的值是什么？"><a href="#问题二：hasNext-和hasNextLine-是怎么判断接下来有无输出的，它返回的值是什么？" class="headerlink" title="问题二：hasNext()和hasNextLine()是怎么判断接下来有无输出的，它返回的值是什么？"></a>问题二：<code>hasNext()</code>和<code>hasNextLine()</code>是怎么判断接下来有无输出的，它返回的值是什么？</h3><p>从问题一中的答案，可以延伸出一些关于这两个函数的理解：<code>hasNext()</code>就是看一下接下来还有无一块字符串传进来；<code>hasNextLine()</code>则是看一下接下来还有无一整行的字符串传进来。<br>那它们都是函数，并且很多人都是把它们写进<code>while()</code>里面来持续输入字符串的，比如<code>while(sc.hasNext())</code>或<code>while(sc.hasNextLine())</code>。那这两个函数是怎么结束输入的啊。<br>我一直以为，<code>hasNext()</code>和<code>hasNextLine()</code>都是在缓冲区中以此扫描各个字符串，如果扫描到有字符串的话就返回<code>true</code>，那么它就会接着输入；如果没有扫描到字符串了，就会返回<code>false</code>，就会跳出<br><code>while()</code>循环了。<del>其实这样也不能解释为什么一开始刚进入while()时，hasNext()和hasNextLine()为什么会返回true，明明我们都还没输入，，，</del><br><strong>然而，事实并不是这样，，，</strong><br>csdn上有一个大佬** @明月即使有666 <strong>的解释我觉得很合理：当执行到<code>hasNext()</code>时，它会先扫描缓冲区中是否有字符，有则返回<code>true</code>,继续扫描。<br>当扫描为空时，</strong>并不返回false**,而是将方法阻塞，等待你输入内容然后继续扫描。<br>看了这段话后，我茅塞顿开。所以它才能在<code>while()</code>中持续检测输入的哇。<del>Java真神奇捏</del><br>并且<code>hasNext()</code>和<code>hasNextLine()</code>里面也可以放一些字符串并取反，让它在检测到特定的字符串时就跳出<code>while()</code>循环结束输入。比如，<code>while(!sc.next(&quot;#&quot;))</code>，当<br>遇到<code>#</code>时，就会跳出循环结束输入。</p>
<hr>
<p>这次热身赛，自己还是学到了东西力，，并且也把以前学过但是忘了的java知识逐渐拾起来了。比如这一次的<code>hasNext()</code>和<code>hasNextLine()</code>的用法，这次比赛出了这道题，<br>让我了解了<code>hasNext()</code>和<code>hasNextLine()</code>这种比较标准的检测输入的方法。如果今天的比赛是蓝桥杯，出了这道会做但是不会结束条件的题，我都会难过死了。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/03/21/hasNext-%E4%B8%8EhasNextLine/" data-id="ckmj2t1ly000060v5elndf3g0" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-力扣-92-反转链表II" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/03/18/%E5%8A%9B%E6%89%A3-92-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8II/" class="article-date">
  <time datetime="2021-03-18T15:46:12.000Z" itemprop="datePublished">2021-03-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2021/03/18/%E5%8A%9B%E6%89%A3-92-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8II/">力扣-92-反转链表II</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="力扣：92-反转链表II"><a href="#力扣：92-反转链表II" class="headerlink" title="力扣：92.反转链表II"></a>力扣：92.反转链表II</h3><p><img src="/images/leetcode-92/img1.jpg" alt="提交记录"><br>先让我秀一波提交记录图。因为这是我第一次靠自己想出来并且通过的第一道难度中等题。芜湖！  </p>
<hr>
<h3 id="题目描述："><a href="#题目描述：" class="headerlink" title="题目描述："></a>题目描述：</h3><p>给你单链表的头指针 <code>head</code> 和两个整数<code>left</code> 和 <code>right</code> ，其中<code>left &lt;= right</code> 。<br>请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点，返回 <strong>反转后的链表</strong> 。  </p>
<h5 id="示例1："><a href="#示例1：" class="headerlink" title="示例1："></a>示例1：</h5><p><img src="/images/leetcode-92/img2.jpg" alt="示例1">  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入：head &#x3D; [1,2,3,4,5], left &#x3D; 2, right &#x3D; 4</span><br><span class="line">输出：[1,4,3,2,5]</span><br></pre></td></tr></table></figure>
<h5 id="示例2："><a href="#示例2：" class="headerlink" title="示例2："></a>示例2：</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入：head &#x3D; [5], left &#x3D; 1, right &#x3D; 1</span><br><span class="line">输出：[5]</span><br></pre></td></tr></table></figure>
<h5 id="提示："><a href="#提示：" class="headerlink" title="提示："></a>提示：</h5><ul>
<li>链表中节点数目为<code>n</code></li>
<li><code>1 &lt;= n &lt;= 500</code></li>
<li><code>-500 &lt;= Node.val &lt;= 500</code></li>
<li><code>1 &lt;= left &lt;= right &lt;= n</code>  </li>
</ul>
<hr>
<h5 id="进阶：你可以使用一趟扫描完成反转吗？"><a href="#进阶：你可以使用一趟扫描完成反转吗？" class="headerlink" title="进阶：你可以使用一趟扫描完成反转吗？"></a>进阶：你可以使用一趟扫描完成反转吗？</h5><p>我的题解：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for singly-linked list.</span></span><br><span class="line"><span class="comment"> * public class ListNode &#123;</span></span><br><span class="line"><span class="comment"> *     int val;</span></span><br><span class="line"><span class="comment"> *     ListNode next;</span></span><br><span class="line"><span class="comment"> *     ListNode() &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     ListNode(int val) &#123; this.val = val; &#125;</span></span><br><span class="line"><span class="comment"> *     ListNode(int val, ListNode next) &#123; this.val = val; this.next = next; &#125;</span></span><br><span class="line"><span class="comment"> * &#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ListNode <span class="title">reverseBetween</span><span class="params">(ListNode head, <span class="keyword">int</span> left, <span class="keyword">int</span> right)</span> </span>&#123;</span><br><span class="line">        ListNode next = head;</span><br><span class="line">        <span class="keyword">boolean</span> flag = <span class="keyword">false</span>;</span><br><span class="line">        Stack&lt;Integer&gt; st = <span class="keyword">new</span> Stack&lt;Integer&gt;();</span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span>(count &lt;= right)&#123;</span><br><span class="line">            <span class="keyword">if</span>(count == left)&#123;</span><br><span class="line">                flag = <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(flag)&#123;</span><br><span class="line">                st.push(next.val);</span><br><span class="line">            &#125;</span><br><span class="line">            next = next.next;</span><br><span class="line">            count++;</span><br><span class="line">        &#125;</span><br><span class="line">        next = head;</span><br><span class="line">        flag = <span class="keyword">false</span>;</span><br><span class="line">        count = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span>(count &lt;= right)&#123;</span><br><span class="line">            <span class="keyword">if</span>(count == left)&#123;</span><br><span class="line">                flag = <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(flag)&#123;</span><br><span class="line">                next.val = st.pop();</span><br><span class="line">            &#125;</span><br><span class="line">            next = next.next;</span><br><span class="line">            count++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> head;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>看示例1可知，输入是一个头指针<code>head</code>、反转的起始位置<code>left</code>和结束位置<code>right</code>。要反转数字的话，<br>那就肯定要记录反转前的数字嘛。所以我就想到了用栈去做。<br>我们定义一个栈<code>Stack&lt;Integer&gt; st = new Stack&lt;Integer&gt;()</code>，并且在第一次循环中，将从<code>left</code>到<br><code>right</code>的数字都押入到栈中。那示例1作为例子，第一次循环结束后，栈内应该是这样的：<br><code>stack = [2, 3, 4]</code>。<br>显然，当栈<code>stack</code>执行<code>pop()</code>操作的话，是把顶部的元素弹出的。所以，我们把<code>stack</code>全部<code>pop()</code>完的话，<br>元素的弹出顺序就应该是<code>[4, 3, 2]</code>。<br>看！这是不是就是反转后的链表！<br>所以，我们只要再进行第二次循环，把从<code>left</code>到<code>right</code>位置的值用<code>stack.pop()</code>重新赋值就可以完成<br>链表的反转了。<br>还有一点，因为<code>left</code>和<code>right</code>给的值的意思是链表的位置。比如，<code>left = 2</code>和<code>right = 4</code>，是指从链表的第2个位置<br>到第4个位置之间的3个结点反转。所以我就设了一个<code>count</code>来计数得出目前所在的位置。（一开始我没注意到这个问题，<br>弄得我两次都提交出错了，，，）</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/03/18/%E5%8A%9B%E6%89%A3-92-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8II/" data-id="ckmf1s8w50000e0v5ecv7cbxw" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-力扣-115-不同的子序列" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/" class="article-date">
  <time datetime="2021-03-17T05:07:29.000Z" itemprop="datePublished">2021-03-17</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/">力扣-115-不同的子序列</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="力扣：115-不同的子序列"><a href="#力扣：115-不同的子序列" class="headerlink" title="力扣：115.不同的子序列"></a>力扣：115.不同的子序列</h3><p>今天的力扣每日一题是《不同的子序列》，这是一道困难题。虽然是困难题，并且一看题目就感觉又又又<br>不会做了，，但是自己还是试着想一想怎么做。毕竟拿到题不思考一下就去看题解，就不叫做题了。<br>首先我马上想到了滑动窗口，然后看到了给的示例，这不对啊，子序列不是连着的也行。接着我看了下这道题的标签，<br>上面写着“动态规划”。啊这，动态规划就是包含现在的数或者不包含现在的数。最后，不行了，还是看题解吧。<br>所幸，有一个人的题解我觉得写得很好，并且浏览数和获赞数还比官方题解高，，<br>下面，我就尝试着用自己理解的意思把这道题解释一下。</p>
<hr>
<h3 id="题目描述："><a href="#题目描述：" class="headerlink" title="题目描述："></a>题目描述：</h3><p>给定一个字符串 <code>s</code> 和一个字符串 <code>t</code> ，计算在 <code>s</code> 的子序列中 <code>t</code> 出现的个数。<br>字符串的一个 子序列 是指，通过删除一些（也可以不删除）字符且不干扰剩余字符相<br>对位置所组成的新字符串。（例如，<code>ACE</code>是<code>ABCDE</code>的一个子序列，而<code>AEC</code>不是）<br>题目数据保证答案符合 <code>32</code> 位带符号整数范围。</p>
<p>示例 1：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">输入：s &#x3D; &quot;rabbbit&quot;, t &#x3D; &quot;rabbit&quot;</span><br><span class="line">输出：3</span><br><span class="line">解释：</span><br><span class="line">如下图所示, 有 3 种可以从 s 中得到 &quot;rabbit&quot; 的方案。</span><br><span class="line">(上箭头符号 ^ 表示选取的字母)</span><br><span class="line">rabbbit</span><br><span class="line">^^^^ ^^</span><br><span class="line">rabbbit</span><br><span class="line">^^ ^^^^</span><br><span class="line">rabbbit</span><br><span class="line">^^^ ^^^</span><br></pre></td></tr></table></figure>

<p>示例 2：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">输入：s &#x3D; &quot;babgbag&quot;, t &#x3D; &quot;bag&quot;</span><br><span class="line">输出：5</span><br><span class="line">解释：</span><br><span class="line">如下图所示, 有 5 种可以从 s 中得到 &quot;bag&quot; 的方案。 </span><br><span class="line">(上箭头符号 ^ 表示选取的字母)</span><br><span class="line">babgbag</span><br><span class="line">^^ ^</span><br><span class="line">babgbag</span><br><span class="line">^^    ^</span><br><span class="line">babgbag</span><br><span class="line">^    ^^</span><br><span class="line">babgbag</span><br><span class="line">  ^  ^^</span><br><span class="line">babgbag</span><br><span class="line">    ^^^</span><br></pre></td></tr></table></figure>

<p>我的代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">numDistinct</span><span class="params">(String s, String t)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">char</span>[] sc = s.toCharArray();</span><br><span class="line">        <span class="keyword">char</span>[] tc = t.toCharArray();</span><br><span class="line">        <span class="keyword">int</span> n_tc = tc.length;</span><br><span class="line">        <span class="keyword">int</span> n_sc = sc.length;</span><br><span class="line">        <span class="keyword">int</span>[][] dp = <span class="keyword">new</span> <span class="keyword">int</span>[n_tc + <span class="number">1</span>][n_sc + <span class="number">1</span>];</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;n_sc+<span class="number">1</span>;i++)&#123;</span><br><span class="line">            dp[<span class="number">0</span>][i] = <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;n_tc+<span class="number">1</span>;i++)&#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>;j&lt;n_sc+<span class="number">1</span>;j++)&#123;</span><br><span class="line">                <span class="keyword">if</span>(tc[i-<span class="number">1</span>] == sc[j-<span class="number">1</span>])&#123;</span><br><span class="line">                    dp[i][j] = dp[i-<span class="number">1</span>][j-<span class="number">1</span>] + dp[i][j-<span class="number">1</span>];</span><br><span class="line">                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                    dp[i][j] = dp[i][j-<span class="number">1</span>];</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> dp[n_tc][n_sc];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这道题得用动态规划来做。<br>我们创建一个<code>dp[t.length() + 1][s.length() + 1]</code>数组来计算我们的子序列个数。为什么要<code>+1</code>呢？<br>因为我们包含了空字符串，空字符串也是一个字符串的子序列，这就像空集是任何一个集合的子集一样。<br>然后，这个数组中的元素<code>dp[i][j]</code>表示：在<code>s</code>的前<code>j</code>个字符中，包含<code>t</code>的前<code>i</code>个字符的个数。<br>接着，我们就能得出一个这样的方程：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">当t[i] &#x3D;&#x3D; s[j]时，dp[i][j] &#x3D; dp[i-1][j-1] + dp[i][j-1]</span><br><span class="line">当t[i] !&#x3D; s[j]时，dp[i][j] &#x3D; dp[i][j-1]</span><br></pre></td></tr></table></figure>
<p>拿<code>s = &quot;babgbag&quot;, t = &quot;bag&quot;</code>来举例，我们可以得出下面的矩阵：<br><img src="/images/leetcode-115/img1.png" alt="leetcode上@powcai的图">  </p>
<p><code>dp[i][j-1]</code>是在<code>s</code>的前<code>j-1</code>的字符中包含<code>t</code>的前<code>i</code>的字符的个数，而<code>dp[i-1][j-1]</code>是<code>s</code>的前<code>j-1</code>的字符中包含<code>t</code>的<br>前<code>i-1</code>的字符的个数，也就是说<code>dp[i-1][j-1]</code>是不包含的<code>t[i]</code>。当<code>t[i] == s[j]</code>时，前面<code>i-1</code>的字符就可以和第<code>i</code>个字符串组合起来，<br>然后再加上<code>[i][j-1]</code>，这个在<code>j-1</code>里面已经能找到i前面字符的个数，就是<code>dp[i][j]</code>的个数。</p>
<hr>
<p>我写起来可能写得不够清楚，但是填一下上面的矩阵，然后再稍微想一下，思路大概就通了。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/" data-id="ckmd1miab0000mov5e548clyi" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-力扣-54-螺旋矩阵" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/03/15/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/" class="article-date">
  <time datetime="2021-03-15T08:17:49.000Z" itemprop="datePublished">2021-03-15</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2021/03/15/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/">力扣: 54.螺旋矩阵</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="力扣：54-螺旋矩阵"><a href="#力扣：54-螺旋矩阵" class="headerlink" title="力扣：54.螺旋矩阵"></a>力扣：54.螺旋矩阵</h2><p>今天的力扣每日一题是《螺旋矩阵》。这道题我在大一大二时都见过，但是每次做的时候都得重新思考如何做。<br>每一次我都做不出来，做不出来就去网上找答案，找了答案也没认真深究过，所以也会忘得很快。<br>今天做这道题的时候也是，一开始想了很久，结果实在想不出来了，最后去看题解了。<br>题解里面有一个人写的方法我觉得非常好，很容易让我这种算法小白理解，因此我决定把它记下来。</p>
<hr>
<h3 id="题目描述：给你一个-m-行-n-列的矩阵-matrix-，请按照-顺时针螺旋顺序-，返回矩阵中的所有元素。"><a href="#题目描述：给你一个-m-行-n-列的矩阵-matrix-，请按照-顺时针螺旋顺序-，返回矩阵中的所有元素。" class="headerlink" title="题目描述：给你一个 m 行 n 列的矩阵 matrix ，请按照 顺时针螺旋顺序 ，返回矩阵中的所有元素。"></a>题目描述：给你一个 m 行 n 列的矩阵 matrix ，请按照 顺时针螺旋顺序 ，返回矩阵中的所有元素。</h3><p><img src="/images/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/spiral1.jpg" alt="示例1">  </p>
<pre><code>输入：matrix = [[1,2,3],[4,5,6],[7,8,9]]  
输出：[1,2,3,6,9,8,7,4,5]</code></pre>
<p><img src="/images/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/spiral2.jpg" alt="示例2"></p>
<pre><code>输入：matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]  
输出：[1,2,3,4,8,12,11,10,9,5,6,7]   </code></pre>
<p>提示：</p>
<ul>
<li>m == matrix.length</li>
<li>n == matrix[i].length</li>
<li>1 &lt;= m, n &lt;= 10</li>
<li>-100 &lt;= matrix[i][j] &lt;= 100  </li>
</ul>
<hr>
<p>我的解题：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;Integer&gt; <span class="title">spiralOrder</span><span class="params">(<span class="keyword">int</span>[][] matrix)</span> </span>&#123;</span><br><span class="line">        List&lt;Integer&gt; list = <span class="keyword">new</span> ArrayList&lt;Integer&gt;();</span><br><span class="line">        <span class="keyword">int</span> m = matrix.length;</span><br><span class="line">        <span class="keyword">int</span> n = matrix[<span class="number">0</span>].length;</span><br><span class="line">        <span class="keyword">int</span> left=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> top=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> right=n-<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> bottom=m-<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> i=<span class="number">0</span>,j=<span class="number">0</span>; <span class="comment">//初始化遍历变量都为0，因为是从[0, 0]开始</span></span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;</span><br><span class="line">            <span class="keyword">for</span>(j=left;j&lt;=right;j++) list.add(matrix[i][j]);</span><br><span class="line">            top++;</span><br><span class="line">            j=right;</span><br><span class="line">            <span class="keyword">if</span>(top&gt;bottom) <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">for</span>(i=top;i&lt;=bottom;i++) list.add(matrix[i][j]);</span><br><span class="line">            right--;</span><br><span class="line">            i=bottom;</span><br><span class="line">            <span class="keyword">if</span>(right&lt;left) <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">for</span>(j=right;j&gt;=left;j--) list.add(matrix[i][j]);</span><br><span class="line">            bottom--;</span><br><span class="line">            j=left;</span><br><span class="line">            <span class="keyword">if</span>(top&gt;bottom) <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">for</span>(i=bottom;i&gt;=top;i--) list.add(matrix[i][j]);</span><br><span class="line">            left++;</span><br><span class="line">            i=top;</span><br><span class="line">            <span class="keyword">if</span>(left&gt;right) <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> list;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们要把一个矩阵螺旋地输出出来，也就是按照顺时针的方向遍历矩阵。<br>我们可以创建四个指针：<code>top, bottom, left, right</code>。并且再设置两个变量<code>i, j</code>来分别负责遍历从<code>top</code>到<code>bottom</code>和从<code>left</code>到<code>right</code>的矩阵。<br>其中，<code>初始化top = 0, bottom = m - 1, left = 0, right = n - 1</code>。<br>螺旋矩阵，即从外到内，遍历每一个元素。从外到内的体现是：遍历了一边的元素后，该边就要向内缩小。<br>比如：一开始<code>top = 0</code>指代的是第一行，那么遍历完第一行的元素后，<code>top++</code>就是让它往内部移动，<br>而<code>bottom--</code>的意思也是如此：第一次遍历完第<code>m-1</code>行后，就往内部移动。<code>left++</code>和<code>right--</code>也是如此。<br>因此，我们可以得到一个遍历结束的条件：当上下边或左右边交叠的时候，就意味着螺旋矩阵已经形成。这体现在<br><code>if(left&gt;right) break;</code>和<code>if(top&gt;bottom) break;</code>，当交叠的时候，就跳出<code>while(true)</code>的循环，然后输出存储螺旋矩阵的<code>list</code>。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/03/15/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/" data-id="ckmadc8uz0000ncv5g6l17ban" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-hello-world" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/11/30/hello-world/" class="article-date">
  <time datetime="2020-11-30T15:22:34.394Z" itemprop="datePublished">2020-11-30</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/11/30/hello-world/">Hello World</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>Welcome to <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a target="_blank" rel="noopener" href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a target="_blank" rel="noopener" href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a target="_blank" rel="noopener" href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">&quot;My New Post&quot;</span></span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/writing.html">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/server.html">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/generating.html">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/11/30/hello-world/" data-id="cki4q72hz0000hwv53ct72brc" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/11/">十一月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/04/18/leetcode-26/">leetcode-26</a>
          </li>
        
          <li>
            <a href="/2021/03/21/hasNext-%E4%B8%8EhasNextLine/">hasNext()与hasNextLine()</a>
          </li>
        
          <li>
            <a href="/2021/03/18/%E5%8A%9B%E6%89%A3-92-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8II/">力扣-92-反转链表II</a>
          </li>
        
          <li>
            <a href="/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/">力扣-115-不同的子序列</a>
          </li>
        
          <li>
            <a href="/2021/03/15/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/">力扣: 54.螺旋矩阵</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 kurodasense<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>